package com.qf.collection.homework;

import com.qf.collection.Cat;
import com.qf.collection.Dog;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 2. Arrays.sort() 方法可以实现对数组进排序，如果传入Cat[]能否排序成功？
 * Exception in thread "main" java.lang.ClassCastException:
 * com.qf.collection.Cat cannot be cast to java.lang.Comparable
 */
public class Homework2 {
    public static void main(String[] args) {

//        Cat[] catArr = new Cat[3];
//        catArr[0] = new Cat("加菲猫111",3);
//        catArr[1] = new Cat("机器猫",100);
//        catArr[2] = new Cat("波斯猫11",2);
//        // 数组转换为集合
//        List<Cat> list = Arrays.asList(catArr);
//        System.out.println(list);
//        Arrays.sort(catArr);
//        System.out.println(Arrays.asList(catArr));

        // 如何不修改 类的源码，也实现排序功能
        Dog[] dogs = new Dog[3];
        dogs[0] = new Dog("史努比", 3);
        dogs[1] = new Dog("沙漠死神", 1);
        dogs[2] = new Dog("汪汪队", 2);
        System.out.println(Arrays.asList(dogs));
        Arrays.sort(dogs, (o1, o2) -> o1.getAge()-o2.getAge());
        System.out.println(Arrays.asList(dogs));

    }
}
